package mark.tom.friend.app;

import android.content.Context;

import com.github.yuweiguocn.library.greendao.MigrationHelper;
import mark.tom.friend.base.application.BaseApplication;
import mark.tom.friend.bean.db.DaoMaster;
import mark.tom.friend.component.AppComponent;
import mark.tom.friend.component.DaggerAppComponent;
import mark.tom.friend.module.AppModule;
import mark.tom.friend.module.DbModule;
import mark.tom.friend.roll.BuildConfig;
import org.greenrobot.greendao.database.Database;

import static mark.tom.friend.consts.DBKeyConsts.DB_KEY_NAME;

public class CustomApplication extends BaseApplication {

    private AppComponent appComponent;

    @Override public void onCreate() {
        super.onCreate();

        MigrationHelper.DEBUG = isApplicationDebuggable();
        DaoMaster.OpenHelper helper = new CustomDBHelper(this, DB_KEY_NAME) ;

        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .dbModule(new DbModule(new DaoMaster(helper.getWritableDatabase()).newSession()))
                .build();
    }

    @Override protected boolean isApplicationDebuggable() {
        return BuildConfig.DEBUG;
    }

    public static CustomApplication get(Context context) {
        return (CustomApplication) context.getApplicationContext();
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }
}
